from osgeo import ogr, osr
import os
import sys
ogr.UseExceptions()

in_path = os.path.join(os.getcwd(), "Include", "dataset", "point.csv")
out_path = os.path.join(os.getcwd(), "Include", "dataset", "shp.shp")
try:
    with open(in_path,"r") as f:
      content = f.readlines()
    f.close()
except FileExistsError:
  sys.exit()

driver: ogr.Driver = ogr.GetDriverByName("Esri shapefile")
dataSource:ogr.DataSource = driver.CreateDataSource(out_path)

# sr
spatialReference:osr.SpatialReference = osr.SpatialReference()
spatialReference.ImportFromEPSG(4326)
# layer
layer: ogr.Layer = dataSource.CreateLayer("point", spatialReference, ogr.wkbPoint)
# field
field_index = ogr.FieldDefn("INDEX", ogr.OFTInteger)
field_index.SetWidth(10)
layer.CreateField(field_index)

field_description = ogr.FieldDefn("DESC", ogr.OFTString)
field_description.SetWidth(10)
layer.CreateField(field_description)

field_shortName = ogr.FieldDefn("SN", ogr.OFTString)
field_shortName.SetWidth(10)
layer.CreateField(field_shortName)

for line in content:
  line_list = line.split(",")
  point:ogr.Geometry = ogr.Geometry(ogr.wkbPoint)
  point.AddPoint(float(line_list[1]), float(line_list[2]))
  #feature
  feature = ogr.Feature(layer.GetLayerDefn())
  feature.SetField("INDEX", int(line_list[0]))
  feature.SetField("DESC", line_list[3])
  feature.SetField("SN", line_list[4].replace("\n",""))

  feature.SetGeometry(point)
  layer.CreateFeature(feature)
  feature = None

dataSource.Destroy()






